package com.qinglei.recoup.system.controller;

import com.excel.poi.ExcelBoot;
import com.excel.poi.function.ExportFunction;
import com.qinglei.recoup.common.annotation.NotBlank;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.system.domain.BedInfoMationDownDataVo;
import com.qinglei.recoup.system.domain.BedInformation;
import com.qinglei.recoup.system.domain.DictBO;
import com.qinglei.recoup.system.pojo.FileParam;
import com.qinglei.recoup.system.service.BedInformationService;
import com.qinglei.recoup.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/bedInformation")
public class BedInformationController {


    @Autowired
    private BedInformationService bedInformationService;


    @PostMapping("/addOrUpdate")
    public CommonRes addOrUpdate(@RequestBody  @Valid BedInformation bedInformation){
        try {
            bedInformationService.addOrUpdate(bedInformation);
        }catch (Exception ex){
            return CommonRes.failed("设备已存在或服务器繁忙");
        }
        return CommonRes.ok();
    }


    @GetMapping("/getByBedId")
    public CommonRes getByBedId(Integer bedId){
        BedInformation bedInformation = bedInformationService.getByBedId(bedId);
        return CommonRes.ok(bedInformation);
    }


    @GetMapping("/getByBedSnCode")
    public CommonRes getByBedSnCode(String snCode){
        BedInformation bedInformation = bedInformationService.getByBedSnCode(snCode);
        return CommonRes.ok(bedInformation);
    }


    @GetMapping("/getType")
    public CommonRes getAllBedInformationType(){
        return  bedInformationService.getAllBedInformationType();
    }



    @GetMapping("/deleteByBedId")
    public CommonRes deleteByBedId(Integer bedId){
        bedInformationService.deleteByBedId(bedId);
        return CommonRes.ok();
    }



    @GetMapping("/deleteBySnCode")
    public CommonRes deleteBySnCode(String snCode){
        bedInformationService.deleteBySnCode(snCode);
        return CommonRes.ok();
    }


    @PostMapping("/uploadFile")
    public CommonRes uploadFile(@RequestParam("file")MultipartFile multipartFile,HttpServletRequest request){
        return  bedInformationService.uploadFile( multipartFile, request);
    }



    @PostMapping("/importBedMsg")
    public CommonRes importBedMsg(@RequestBody FileParam filePath){
        return  bedInformationService.importBedMsg(filePath.getFilePath(),filePath.getIsImport());
    }


    @GetMapping("/downProblemData")
    public void downProblemData(HttpServletResponse httpServletResponse,FileParam filePath){
        //筛选出问题的数据
        BedInfoMationDownDataVo bedInfoMationDownDataVo = new BedInfoMationDownDataVo();
        ExcelBoot.ExportBuilder(httpServletResponse, "批量导入床位错误数据", BedInfoMationDownDataVo.class).exportResponse(bedInfoMationDownDataVo,
                new ExportFunction<BedInfoMationDownDataVo, BedInfoMationDownDataVo>() {
                    /**
                     * @param queryQaram 查询条件对象
                     * @param pageNum    当前页数,从1开始
                     * @param pageSize   每页条数,默认3000
                     * @return
                     */
                    @Override
                    public List<BedInfoMationDownDataVo> pageQuery(BedInfoMationDownDataVo queryQaram, int pageNum, int pageSize) {
                        //1.将pageNum和pageSize传入使用本组件的开发者自己项目的分页逻辑中
                        //2.调用使用本组件的开发者自己项目的分页查询方法
                        try {
                            return bedInformationService.downProblemData(filePath);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                    /**
                     * 将查询出来的每条数据进行转换
                     *
                     * @param o
                     */
                    @Override
                    public BedInfoMationDownDataVo convert(BedInfoMationDownDataVo o) {
                        //用于编写开发者自定义的转换逻辑
                        return o;
                    }
                });
    }



}
